package cn.iocoder.yudao.module.infra.controller.admin.ocr;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.config.OcrConfigPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.config.OcrConfigRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.config.OcrConfigSaveReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.config.OcrConfigSimpleRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.ocr.OcrConfigDO;
import cn.iocoder.yudao.module.infra.service.ocr.OcrConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - OCR配置")
@RestController
@RequestMapping("/infra/ocr-config")
@Validated
public class OcrConfigController {

    @Resource
    private OcrConfigService ocrConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建OCR配置")
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:create')")
    public CommonResult<Long> createOcrConfig(@Valid @RequestBody OcrConfigSaveReqVO createReqVO) {
        return success(ocrConfigService.createOcrConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OCR配置")
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:update')")
    public CommonResult<Boolean> updateOcrConfig(@Valid @RequestBody OcrConfigSaveReqVO updateReqVO) {
        ocrConfigService.updateOcrConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OCR配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:delete')")
    public CommonResult<Boolean> deleteOcrConfig(@RequestParam("id") Long id) {
        ocrConfigService.deleteOcrConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除OCR配置")
    @Parameter(name = "ids", description = "编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:delete')")
    public CommonResult<Boolean> deleteOcrConfigList(@RequestParam("ids") List<Long> ids) {
        ocrConfigService.deleteOcrConfigList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OCR配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:query')")
    public CommonResult<OcrConfigRespVO> getOcrConfig(@RequestParam("id") Long id) {
        OcrConfigDO config = ocrConfigService.getOcrConfig(id);
        return success(BeanUtils.toBean(config, OcrConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OCR配置分页")
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:query')")
    public CommonResult<PageResult<OcrConfigRespVO>> getOcrConfigPage(@Valid OcrConfigPageReqVO pageVO) {
        PageResult<OcrConfigDO> pageResult = ocrConfigService.getOcrConfigPage(pageVO);
        return success(BeanUtils.toBean(pageResult, OcrConfigRespVO.class));
    }

    @GetMapping("/test")
    @Operation(summary = "Test OCR configuration")
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:query')")
    public CommonResult<String> testOcrConfig(@RequestParam("id") Long id) {
        String result = ocrConfigService.testOcrConfig(id);
        return success(result);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "Get OCR configuration simple list")
    @PreAuthorize("@ss.hasPermission('infra:ocr-config:query')")
    public CommonResult<List<OcrConfigSimpleRespVO>> getOcrConfigSimpleList() {
        List<OcrConfigSimpleRespVO> list = ocrConfigService.getOcrConfigSimpleList();
        return success(list);
    }

}